<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="demoTable layui-form">
            缴费状态：
            <div class="layui-inline">
                <select name="status" id="status">
                    <option value="2">请选择</option>
                    <option value="1">已缴费</option>
                    <option value="0">未缴费</option>
                </select>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                        lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                </button>
            </div>
        </div>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var id;
    layui.use(['form', 'table',  'element', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;
            // miniPage = layui.miniPage;
        // form.render();
        table.render({
            elem: '#currentTableId',
            url: '/user?method=selectPageCarInfo',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 100, title: 'ID', sort: true},
                {field: 'carnum', width: 100, title: '车位号码'},
                {field: 'caruser', width: 100, title: '户主'},
                {field: 'starttime', width: 150, title: '开始时间'},
                {field: 'endtime', width: 150, title: '结束时间'},
                {field: 'money', width: 80, title: '金额'},
                {field: 'status', width: 100, title: '状态',templet:'#titleTpl'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"},
            ]],
            limits: [10, 15, 20, 25],
            limit: 15,
            page: true,
            skin: 'line',
            id: 'testReload'
        });
        form.render();
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作

                var index = layer.open({
                    title: '添加车位信息',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['90%', '90%'],
                    offset:'auto',//坐标上，左
                    content: 'addPark.jsp',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                console.log(data)
                var ids = data.map(user => {
                    return user.id;
                }).join(',')
                console.log(ids)
                if (data.length == 0) {
                    layer.msg("请至少选择一条数据")
                    return;
                }
                layer.alert("确定要删除选中的数据吗?", function () {
                    $.post('/user?method=deleteCarInfo', {ids}, function (res) {
                        if (res.code == 200) {
                            layer.msg(res.msg, function () {
                                // 重载数据表格
                                table.reload('testReload');
                            })
                        } else {
                            layer.msg(res.msg)
                        }
                    })
                })
            }
        });

        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('testReload', {
                page: {
                    curr: 1
                }
                , where: {    //  发送请求时 携带的参数
                    searchParams: result   // string
                }
            }, 'data');
            return false;
        });

        function setFormValues(data) {
            form.val("updateSubmit", {
                "id": data.id,
                "carnum": data.carnum,
                "caruser": data.caruser,
                "starttime": data.starttime,
                "endtime": data.endtime,
                "money": data.money,
                "status": data.status,
            });
        };
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                id = data.id;
                var index = layer.open({

                    title: '编辑车位信息',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['90%', '90%'],
                    offset:'auto',//坐标上，左
                    content: 'editPark.jsp'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;

            } else if (obj.event === 'delete') {
                let  ids = data.id;
                layer.confirm('确认删除?', function (index) {
                    $.post('/user?method=deleteCarInfo',{ids},function (res) {
                        if (res.code == 200){
                            layer.msg(res.msg,function (){
                                // 重载数据表格
                                table.reload('testReload');
                            })
                        }else{
                            layer.msg(res.msg)
                        }
                    })
                });
            }
        });

    });
</script>
<script type="text/html" id="titleTpl">
    {{#  if(d.status == '未缴费'){ }}
    <span style="color: red;">{{ d.status }}</span>
    {{#  } else { }}
    <span style="color: grey;">{{ d.status }}</span>
    {{#  } }}
</script>

</body>
</html>